package service.report.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.app.AppFinance;
import dao.LoaninterestrecordDao;
import dao.app.AppFinanceDao;
import service.report.FinancialStatusService;
import util.MoneyCalc;

public class FinancialStatusServiceImpl implements FinancialStatusService {
	private AppFinanceDao financeDao;
	private LoaninterestrecordDao interestrecordDao;
	private String searchDate;
	
	public AppFinance findOne(String str) throws Exception{
		String hql="DATE_FORMAT(financedate, '%Y-%m') = DATE_FORMAT('" + str + "', '%Y-%m')";
		AppFinance finance=financeDao.findByOne(hql);
		if(finance==null) throw new Exception("财务数据获取异常！此月份没有数据。");
		return finance;
	}
	public List<AppFinance> findList(String str){
		List<AppFinance> list=financeDao.findModel("DATE_FORMAT(financedate, '%Y') = DATE_FORMAT('" + str + "', '%Y')");
		return list;
	}
	public BigDecimal countInterest(int type,String str){
		String select="new vo.MoneyDoubleBean(l.grantmoney)";
		String from="Loaninterestrecord as l where l.state=0";
		if(type==1){
			from=from+" and DATE_FORMAT(l.confirmdate, '%Y-%m') = DATE_FORMAT('" + str + "', '%Y-%m')";
		}
		if(type==2){
			from=from+" and DATE_FORMAT(l.confirmdate, '%Y') = DATE_FORMAT('" + str + "', '%Y')";
		}
		List list=interestrecordDao.findQuery(select, from);
		BigDecimal interest=MoneyCalc.moneyTotal(list).setScale(2, BigDecimal.ROUND_HALF_UP);
		return interest;
	}
	
	public Map<Object,Object> getFinanceReportMap(String searchDate) throws Exception{
		this.searchDate=searchDate;
		Map<Object,Object> FinanceMap = new HashMap<Object,Object>();
		AppFinance finance=this.findOne(searchDate);
		AppFinance af=new AppFinance();
		af.setOperatregfinance(new BigDecimal(finance.getOperatregfinance()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setOperatorgfinance(new BigDecimal(finance.getOperatorgfinance()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setAsset(new BigDecimal(finance.getAsset()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setDebt(new BigDecimal(finance.getDebt()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setTaxincome(new BigDecimal(finance.getTaxincome()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setTaxbusiness(new BigDecimal(finance.getTaxbusiness()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setTaxstamp(new BigDecimal(finance.getTaxstamp()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setTaxother(new BigDecimal(finance.getTaxother()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setProfitcurrent(new BigDecimal(finance.getProfitcurrent()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setProfitreserve(new BigDecimal(finance.getProfitreserve()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setWorkemployee(new BigDecimal(finance.getWorkemployee()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		af.setWorkmanager(new BigDecimal(finance.getWorkmanager()).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
		FinanceMap.put("finance", af);
		
		BigDecimal operatingfunds=new BigDecimal(finance.getOperatorgfinance()).add(new BigDecimal(finance.getOperatregfinance())).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP);
		FinanceMap.put("operatingfunds", operatingfunds);//营运资金小计
		
		BigDecimal ownershipinterest=new BigDecimal(finance.getAsset()).subtract(new BigDecimal(finance.getDebt())).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP);
		FinanceMap.put("ownershipinterest",ownershipinterest );//所有者权益
		
		BigDecimal tax=new BigDecimal(finance.getTaxincome()).add(new BigDecimal(finance.getTaxbusiness())).add(new BigDecimal(finance.getTaxstamp())).add(new BigDecimal(finance.getTaxother())).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP);
		FinanceMap.put("tax", tax);//当月上缴小计
		
		List<AppFinance> list=this.findList(searchDate);
		BigDecimal taxincometotal=new BigDecimal(0);
		BigDecimal taxbusinesstotal=new BigDecimal(0);
		BigDecimal taxstamptotal=new BigDecimal(0);
		BigDecimal taxothertotal=new BigDecimal(0);
		BigDecimal profitcurrenttotal=new BigDecimal(0);
		for(AppFinance f:list){
			taxincometotal=taxincometotal.add(new BigDecimal(f.getTaxincome())).setScale(1, BigDecimal.ROUND_HALF_UP);
			taxbusinesstotal=taxbusinesstotal.add(new BigDecimal(f.getTaxbusiness())).setScale(1, BigDecimal.ROUND_HALF_UP);
			taxstamptotal=taxstamptotal.add(new BigDecimal(f.getTaxstamp())).setScale(1, BigDecimal.ROUND_HALF_UP);
			taxothertotal=taxothertotal.add(new BigDecimal(f.getTaxother())).setScale(1, BigDecimal.ROUND_HALF_UP);
			profitcurrenttotal=profitcurrenttotal.add(new BigDecimal(f.getProfitcurrent())).setScale(1, BigDecimal.ROUND_HALF_UP);
		}
		FinanceMap.put("taxincometotal", taxincometotal.divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP));//累计企业所得税
		FinanceMap.put("taxbusinesstotal", taxbusinesstotal.divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP));//累计营业税及附加
		FinanceMap.put("taxstamptotal", taxstamptotal.divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP));//印花税
		FinanceMap.put("taxothertotal", taxothertotal.divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP));//其他
		FinanceMap.put("profitcurrenttotal", profitcurrenttotal.divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP));//累计营业利润
		BigDecimal taxtotal=taxincometotal.add(taxbusinesstotal).add(taxstamptotal).add(taxothertotal).divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP);
		FinanceMap.put("taxtotal", taxtotal);//本年累计上缴小计
		
		BigDecimal interestmonth=this.countInterest(1, searchDate);
		FinanceMap.put("interestmonth", interestmonth.divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP));//当月利息
		
		BigDecimal interesttotal=this.countInterest(2, searchDate);
		FinanceMap.put("interesttotal", interesttotal.divide(new BigDecimal(10000)).setScale(1, BigDecimal.ROUND_HALF_UP));//当年累计利息
		return FinanceMap; 	
	}

	public AppFinanceDao getFinanceDao() {
		return financeDao;
	}

	public void setFinanceDao(AppFinanceDao financeDao) {
		this.financeDao = financeDao;
	}
	public String getSearchDate() {
		return searchDate;
	}
	public void setSearchDate(String searchDate) {
		this.searchDate = searchDate;
	}
	public LoaninterestrecordDao getInterestrecordDao() {
		return interestrecordDao;
	}
	public void setInterestrecordDao(LoaninterestrecordDao interestrecordDao) {
		this.interestrecordDao = interestrecordDao;
	}
	

}
